package com.iursp.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.CollectionUtils;

import com.iursp.conf.annotation.CacheLock;
import com.iursp.conf.annotation.CacheParam;
import com.iursp.domain.model.UihPage;
import com.iursp.domain.model.role.AuthPlatform;
import com.iursp.domain.model.role.Role;
import com.iursp.domain.model.role.TreeNode;
import com.iursp.domain.model.user.User;
import com.iursp.domain.result.ExceptionMsg;
import com.iursp.domain.result.Response;
import com.iursp.service.role.AuthRoleService;
import com.iursp.service.role.PlatformService;
import com.iursp.service.user.UserService;
import com.iursp.utils.MD5Util;
import com.iursp.utils.ResultUtils;
import com.uih.imagecloud.frame.core.utils.Md5Utils;

import groovy.util.logging.Slf4j;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/auth/role")
@Slf4j
public class UserRoleController extends BaseController {
	
	@Autowired
	private PlatformService platformService;
	
	@Autowired
	private AuthRoleService roleService;
	
	@Autowired
	private UserService userService;
	
	/**
	 * 获取角色节点信息
	 * @param id
	 * @return
	 */
	@ApiOperation(value="获取角色节点信息", notes="1.获取系统平台列表 2.获取角色列表 展示在最左侧树形菜单")
	@GetMapping(value = "/")
	public List<TreeNode> getRoleTreeNode() {
		/**
		 * 此节点需分两步获取 1.获取系统平台列表 2.获取角色列表
		 */
		try { 
			List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
			List<AuthPlatform> platformList = platformService.queryInnerPlatform();
			if(!CollectionUtils.isEmpty(platformList)){
				for (AuthPlatform sysPlatform : platformList) {
					TreeNode treeNode = new TreeNode();
					treeNode.setId(sysPlatform.getRefid());
					treeNode.setName(sysPlatform.getPlatformName());
					treeNode.setObj(sysPlatform);
					treeNodeList.add(treeNode);
				}
			}
			
			List<Role> roleList = roleService.queryAllRole();
			if (!CollectionUtils.isEmpty(roleList)) {
				for (Role role : roleList) {
					TreeNode treeNode = new TreeNode();
					treeNode.setId(role.getRefid());
					treeNode.setName(role.getRoleName());
					treeNode.setPid(role.getPlatformRefid());
					treeNode.setObj(role);
					treeNodeList.add(treeNode);
				}
			}
			return treeNodeList;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取角色节点发生异常,错误信息为:", e);
		}
		return null;
	}
	
	/**
	 * 
	 * @Title: saveRoleInfo   
	 * @Description: <pre>保存角色节点信息</pre>  
	 * @param: @param role
	 * @param: @return      
	 * @return: Response      
	 * @throws
	 */
	@ApiOperation(value="保存角色节点信息", notes="分新增和修改，以是否传入refid区分")
	@RequestMapping(value = "/saveRoleInfo", method = { RequestMethod.GET, RequestMethod.POST })
	@CacheLock(prefix = "roles")
	public Response saveRoleInfo(Role role) {
		try {
			role.setRoleRowKey(Md5Utils.md516(role.getRoleCode()));
			if (StringUtils.isNotBlank(role.getRefid())) {
				roleService.updateRole(role);
			} else {
				role.setRefid(MD5Util.encrypt());
				roleService.addRole(role);
			}
			return ResultUtils.result(ExceptionMsg.SUCCESS);
		} catch (Exception e) {
			logger.error("保存角色信息异常,信息为:",e);
		}
		return ResultUtils.result(ExceptionMsg.NewUserRole);
	}
	
	/**
	 * 
	 * @Title: deleteRoleInfo   
	 * @Description: 删除角色节点信息  
	 * @param: @param refid
	 * @param: @return      
	 * @return: Response      
	 * @throws
	 */
	@ApiOperation(value="删除角色节点信息  ", notes="以refid删除角色节点信息  ")
	@RequestMapping(value="/{refid}", method=RequestMethod.DELETE,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@CacheLock(prefix = "roles")
	public Response deleteRoleInfo(@PathVariable String refid,@CacheParam(name = "token") @RequestParam(value="token",required=false) String token) {
		try {
			roleService.deleteRoleByRefid(refid);
			return ResultUtils.result(ExceptionMsg.SUCCESS);
		} catch (Exception e) {
			logger.error("删除角色信息异常,信息为:",e);
		}
		return ResultUtils.result(ExceptionMsg.FAILED);
	}
	
	
	/**
	 * 
	 * @Title: getRoleUserList   
	 * @Description: 根据角色获取用户   
	 * @param: @param role
	 * @param: @return      
	 * @return: PageInfo<User>      
	 * @throws
	 */
	@ApiOperation(value="根据角色获取用户     ", notes="根据角色获取用户   并以table展示  ")
	@GetMapping(value = "/getRoleUserList")
	public UihPage<User> getRoleUserList(Role role,@RequestParam Integer limit,@RequestParam Integer offset) {
		try {
			UihPage<User> uihPage = userService.getRoleUserListPage(role,limit,offset);
			return uihPage;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("根据角色信息分页获取用户信息异常,信息为:",e);
			return new UihPage<User>(); 
		}
	}
	
}
